﻿// <copyright file="StarRating.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace MediaRss
{
    #region

    using System.Xml;
    using System.Xml.Serialization;

    #endregion

    [XmlRoot(ElementName = "starRating", Namespace = "http://search.yahoo.com/mrss/")]
    public class StarRating : MediaRssBase, IXmlSerializable
    {
        #region Constants and Fields

        internal const string ELEMENT_NAME = "starRating";

        #endregion

        #region Public Properties

        public double Average { get; set; }

        public int Count { get; set; }

        public double Max { get; set; }

        public double Min { get; set; }

        #endregion

        #region Public Methods

        public void ReadXml(XmlReader reader)
        {
            bool isEmptyElement = reader.IsEmptyElement;
            if (reader.HasAttributes)
            {
                for (int i = 0; i < reader.AttributeCount; i++)
                {
                    reader.MoveToNextAttribute();
                    if (reader.NamespaceURI == "")
                    {
                        string localName = reader.LocalName;
                        if (localName == null)
                        {
                            goto Label_00D6;
                        }
                        if (!(localName == "average"))
                        {
                            if (localName == "min")
                            {
                                goto Label_009A;
                            }
                            if (localName == "max")
                            {
                                goto Label_00AE;
                            }
                            if (localName == "count")
                            {
                                goto Label_00C2;
                            }
                            goto Label_00D6;
                        }
                        this.Average = double.Parse(reader.Value);
                    }
                    goto Label_00FC;
                    Label_009A:
                    this.Min = double.Parse(reader.Value);
                    goto Label_00FC;
                    Label_00AE:
                    this.Max = double.Parse(reader.Value);
                    goto Label_00FC;
                    Label_00C2:
                    this.Count = int.Parse(reader.Value);
                    goto Label_00FC;
                    Label_00D6:
                    base.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                    Label_00FC:
                    ;
                }
            }
            reader.ReadStartElement();
            base.AddElementExtensions(reader, isEmptyElement);
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("media", "starRating", "http://search.yahoo.com/mrss/");
            writer.WriteAttributeString("average", null, this.Average.ToString());
            writer.WriteAttributeString("min", null, this.Min.ToString());
            writer.WriteAttributeString("max", null, this.Max.ToString());
            writer.WriteAttributeString("count", null, this.Count.ToString());
            foreach (var pair in base.AttributeExtensions)
            {
                writer.WriteAttributeString(pair.Key.Name, pair.Key.Namespace, pair.Value);
            }
            writer.WriteEndElement();
            foreach (var element in base.ElementExtensions)
            {
                element.WriteTo(writer);
            }
        }

        #endregion
    }
}